// index.js
import {getBannerList,getDynamic,getIndexNav,getMianshaList,getGoods} from '../../api/home'
let app=getApp();
Page({
  data: {
    bannerList:[] ,//大轮播的数据
    DynamicList:[],//购买滚动的数据
    navList:[],//首页导航的数据
    miaoshaList:[],//秒杀数据
    baoList:[],//爆款数据
    page:1, //页码
    pageSize:4,//每页的条数
    goodList:[],//商品列表
    totalPage:0,//总页数
    coverFlag:true,//遮罩广告显示隐藏
  },
  // 关闭遮罩
  close(){
    this.setData({
      coverFlag:false
    })
  },
  turnSea(){ //跳转搜索页
      wx.navigateTo({
        url: '/pages/search/search'
      })
  },
  turnCate(e){
    let id=e.currentTarget.dataset.id;
    let index=e.currentTarget.dataset.index
    app.globalData.cid=id;
    app.globalData.cindex=index;
    wx.switchTab({
      url: '/pages/category/category',
    })
  },
  onLoad(){
  
    this.InitBanner()
    this.getDyna()
    this.getNav()
    this.getMiaosha()
    this.getBaoKuan()
    this.getGoodList()
  },
  onShow(){},
  async InitBanner(){ //初始化banner
    let res=await getBannerList('index')
    console.log(res)
    // 存储接口返回的数据 用来渲染轮播图
    this.setData({ 
      bannerList:res.data
    })
  },
  turnInfo(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url:"/pages/goodDetail/goodDetail?id="+id
    })
  },
  // 获取滚动购买信息
  async getDyna(){
    let res=await getDynamic({type:0})
    console.log(res)
    this.setData({
      DynamicList:res.data
    })
  },
  // 获取首页导航的方法
  async getNav(){
    let res=await getIndexNav();
    console.log(res,"--------------")
    this.setData({
      navList:res.data
    })
  },
  // 获取秒杀数据
  async getMiaosha(){
    let res=await getMianshaList({
      miaosha:true
    })
    res.data.result.length=2;
    this.setData({
      miaoshaList:res.data.result
    })
    console.log(res,"********")
  },
  // 爆款推荐数据获取
  async getBaoKuan(){
    let res=await getGoods({
      recommendStatus:1
    })
    console.log(res,"99999999999")
    this.setData({
      baoList:res.data.result
    })
  },
  // 获取商品列表
  async getGoodList(){
    let res=await getGoods({
      page:this.data.page,
      pageSize:this.data.pageSize
    })
    console.log(res,"000000000000")
    let arr=[...this.data.goodList,...res.data.result];
    this.setData({
      goodList:arr,
      totalPage:res.data.totalPage
    })
  },
  onReachBottom(){
    this.setData({
      page:++this.data.page
    })
    this.getGoodList();
  }
})
